package com.yuqing.sentimentapp.workspace.action;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.androidkun.xtablayout.XTabLayout;
import com.linkface.baselibrary.http.BaseBean;
import com.yuqing.sentimentapp.R;
import com.yuqing.sentimentapp.base.BaseActivity;
import com.yuqing.sentimentapp.base.BaseObserver;
import com.yuqing.sentimentapp.base.BaseProgressView;
import com.yuqing.sentimentapp.module.ModuleManager;
import com.yuqing.sentimentapp.workspace.action.entity.ActionInfoResult;
import com.yuqing.sentimentapp.workspace.action.entity.ActionReportResult;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by shenhe on 2020/4/3.
 */
public class ActionFillInActivity extends BaseActivity implements BaseProgressView<BaseBean> {

    @BindView(R.id.iv_back)
    ImageView mIvBack;
    @BindView(R.id.tv_save)
    TextView mTvSave;
    @BindView(R.id.tabs)
    XTabLayout mTabs;
    @BindView(R.id.et_fire_count)
    EditText mEtFireCount;
    @BindView(R.id.et_fire_action_count)
    EditText mEtFireActionCount;
    @BindView(R.id.et_fire_car_count)
    EditText mEtFireCarCount;
    @BindView(R.id.et_fire_people_count)
    EditText mEtFirePeopleCount;
    @BindView(R.id.et_fire_dead_count)
    EditText mEtFireDeadCount;
    @BindView(R.id.et_fire_injured_count)
    EditText mEtFireInjuredCount;
    @BindView(R.id.et_fire_saved_count)
    EditText mEtFireSavedCount;
    @BindView(R.id.et_fire_survive_count)
    EditText mEtFireSurviveCount;
    @BindView(R.id.et_fire_evacuate_count)
    EditText mEtFireEvacuateCount;
    @BindView(R.id.et_rescue_count)
    EditText mEtRescueCount;
    @BindView(R.id.et_rescue_action_count)
    EditText mEtRescueActionCount;
    @BindView(R.id.et_rescue_car_count)
    EditText mEtRescueCarCount;
    @BindView(R.id.et_rescue_people_count)
    EditText mEtRescuePeopleCount;
    @BindView(R.id.et_rescue_dead_count)
    EditText mEtRescueDeadCount;
    @BindView(R.id.et_rescue_injured_count)
    EditText mEtRescueInjuredCount;
    @BindView(R.id.et_rescue_saved_count)
    EditText mEtRescueSavedCount;
    @BindView(R.id.et_rescue_survive_count)
    EditText mEtRescueSurviveCount;
    @BindView(R.id.ll_fire)
    LinearLayout mLlFire;
    @BindView(R.id.ll_rescue)
    LinearLayout mLlRescue;
    private boolean editEnable = true;
    /**
     * 点击填报跳转过来的
     */
    private ActionInfoResult.DataBean actionInfo;
    /**
     * 从已上报列表跳转过来的
     */
    private ActionReportResult.DataBean.ReportsBean reportsInfo;
    private ActionReportResult.DataBean.SubSumBean subSum;

    public static void goActivity(Activity activity, ActionInfoResult.DataBean actionInfo, ActionReportResult.DataBean.SubSumBean subSum) {
        Intent intent = new Intent(activity, ActionFillInActivity.class);
        intent.putExtra("actionInfo", actionInfo);
        intent.putExtra("subSum", subSum);
        activity.startActivity(intent);
    }

    public static void goActivity(Activity activity, ActionReportResult.DataBean.ReportsBean reportsInfo) {
        Intent intent = new Intent(activity, ActionFillInActivity.class);
        intent.putExtra("reportsInfo", reportsInfo);
        activity.startActivity(intent);
    }


    @Override
    public int getLayoutId() {
        return R.layout.activity_action_fill_in;
    }

    @Override
    public void initPresenter() {
        Intent intent = getIntent();
        if (intent == null) return;
        actionInfo = ((ActionInfoResult.DataBean) intent.getSerializableExtra("actionInfo"));
        subSum = ((ActionReportResult.DataBean.SubSumBean) intent.getSerializableExtra("subSum"));

        reportsInfo = ((ActionReportResult.DataBean.ReportsBean) intent.getSerializableExtra("reportsInfo"));

    }

    @Override
    public void initView() {
        if (actionInfo != null) {
            mEtFireCount.setText(actionInfo.getFireCount());
            mEtFireActionCount.setText(actionInfo.getFireActionCount());
            mEtFireCarCount.setText(actionInfo.getFireCarCount());
            mEtFirePeopleCount.setText(actionInfo.getFirePeopleCount());
            mEtFireDeadCount.setText(actionInfo.getFireDeadCount());
            mEtFireSavedCount.setText(actionInfo.getFireSavedCount());
            mEtFireInjuredCount.setText(actionInfo.getFireInjuredCount());
            mEtFireSurviveCount.setText(actionInfo.getFireSurviveCount());
            mEtFireEvacuateCount.setText(actionInfo.getFireEvacuateCount());

            mEtRescueCount.setText(actionInfo.getRescueCount());
            mEtRescueActionCount.setText(actionInfo.getRescueActionCount());
            mEtRescueCarCount.setText(actionInfo.getRescueCarCount());
            mEtRescuePeopleCount.setText(actionInfo.getRescuePeopleCount());
            mEtRescueDeadCount.setText(actionInfo.getRescueDeadCount());
            mEtRescueInjuredCount.setText(actionInfo.getRescueInjuredCount());
            mEtRescueSavedCount.setText(actionInfo.getRescueSavedCount());
            mEtRescueSurviveCount.setText(actionInfo.getRescueSurviveCount());
        } else {
            if (subSum != null) {
                mEtFireCount.setText(subSum.getFireCount());
                mEtFireActionCount.setText(subSum.getFireActionCount());
                mEtFireCarCount.setText(subSum.getFireCarCount());
                mEtFirePeopleCount.setText(subSum.getFirePeopleCount());
                mEtFireDeadCount.setText(subSum.getFireDeadCount());
                mEtFireSavedCount.setText(subSum.getFireSavedCount());
                mEtFireInjuredCount.setText(subSum.getFireInjuredCount());
                mEtFireSurviveCount.setText(subSum.getFireSurviveCount());
                mEtFireEvacuateCount.setText(subSum.getFireEvacuateCount());

                mEtRescueCount.setText(subSum.getRescueCount());
                mEtRescueActionCount.setText(subSum.getRescueActionCount());
                mEtRescueCarCount.setText(subSum.getRescueCarCount());
                mEtRescuePeopleCount.setText(subSum.getRescuePeopleCount());
                mEtRescueDeadCount.setText(subSum.getRescueDeadCount());
                mEtRescueInjuredCount.setText(subSum.getRescueInjuredCount());
                mEtRescueSavedCount.setText(subSum.getRescueSavedCount());
                mEtRescueSurviveCount.setText(subSum.getRescueSurviveCount());
            }
        }

        /**
         * 从已上报列表跳转过来的，不可编辑，隐藏保存按钮
         */
        if (reportsInfo != null) {
            mEtFireCount.setText(reportsInfo.getFireCount());
            mEtFireActionCount.setText(reportsInfo.getFireActionCount());
            mEtFireCarCount.setText(reportsInfo.getFireCarCount());
            mEtFirePeopleCount.setText(reportsInfo.getFirePeopleCount());
            mEtFireDeadCount.setText(reportsInfo.getFireDeadCount());
            mEtFireSavedCount.setText(reportsInfo.getFireSavedCount());
            mEtFireInjuredCount.setText(reportsInfo.getFireInjuredCount());
            mEtFireSurviveCount.setText(reportsInfo.getFireSurviveCount());
            mEtFireEvacuateCount.setText(reportsInfo.getFireEvacuateCount());

            mEtRescueCount.setText(reportsInfo.getRescueCount());
            mEtRescueActionCount.setText(reportsInfo.getRescueActionCount());
            mEtRescueCarCount.setText(reportsInfo.getRescueCarCount());
            mEtRescuePeopleCount.setText(reportsInfo.getRescuePeopleCount());
            mEtRescueDeadCount.setText(reportsInfo.getRescueDeadCount());
            mEtRescueInjuredCount.setText(reportsInfo.getRescueInjuredCount());
            mEtRescueSavedCount.setText(reportsInfo.getRescueSavedCount());
            mEtRescueSurviveCount.setText(reportsInfo.getRescueSurviveCount());

            mEtFireCount.setEnabled(false);
            mEtFireActionCount.setEnabled(false);
            mEtFireCarCount.setEnabled(false);
            mEtFirePeopleCount.setEnabled(false);
            mEtFireDeadCount.setEnabled(false);
            mEtFireSavedCount.setEnabled(false);
            mEtFireInjuredCount.setEnabled(false);
            mEtFireSurviveCount.setEnabled(false);
            mEtFireEvacuateCount.setEnabled(false);

            mEtRescueCount.setEnabled(false);
            mEtRescueActionCount.setEnabled(false);
            mEtRescueCarCount.setEnabled(false);
            mEtRescuePeopleCount.setEnabled(false);
            mEtRescueDeadCount.setEnabled(false);
            mEtRescueInjuredCount.setEnabled(false);
            mEtRescueSavedCount.setEnabled(false);
            mEtRescueSurviveCount.setEnabled(false);

            mTvSave.setVisibility(View.GONE);
        }

        mTabs.setOnTabSelectedListener(new XTabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(XTabLayout.Tab tab) {
                resetView();
                if (tab.getPosition() == 0) {
                    mLlFire.setVisibility(View.VISIBLE);
                } else if (tab.getPosition() == 1) {
                    mLlRescue.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onTabUnselected(XTabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(XTabLayout.Tab tab) {

            }
        });
    }

    private void resetView() {
        mLlFire.setVisibility(View.GONE);
        mLlRescue.setVisibility(View.GONE);
    }

    @OnClick({R.id.iv_back, R.id.tv_save})
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_save:
                if (actionInfo == null) {
                    actionInfo = new ActionInfoResult.DataBean();
                    setData();
                    saveActionInfo(actionInfo);
                } else {
                    setData();
                    updateActionInfo(actionInfo);
                }
                break;
        }
    }

    private void updateActionInfo(ActionInfoResult.DataBean actionInfo) {
        ModuleManager.getInstance().updateActionReportInfoResult(actionInfo)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseBean>() {
                    @Override
                    protected void hideDialog() {
                        stopProgressDialog();
                    }

                    @Override
                    protected void showDialog() {
                        startProgressDialog();
                    }

                    @Override
                    protected void onSuccess(BaseBean baseBean) {
                        showShortToast("保存成功");
                        finish();
                    }

                    @Override
                    protected void onFailure(String message) {
                        showShortToast(message);
                    }
                });

    }

    private void saveActionInfo(ActionInfoResult.DataBean actionInfo) {
        ModuleManager.getInstance().saveActionReportInfoResult(actionInfo)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseBean>() {
                    @Override
                    protected void hideDialog() {
                        stopProgressDialog();
                    }

                    @Override
                    protected void showDialog() {
                        startProgressDialog();
                    }

                    @Override
                    protected void onSuccess(BaseBean baseBean) {
                        showShortToast("保存成功");
                        finish();
                    }

                    @Override
                    protected void onFailure(String message) {
                        showShortToast(message);
                    }
                });

    }

    private void setData() {
        actionInfo.setFireCount(TextUtils.isEmpty(mEtFireCount.getText().toString()) ? "0" : mEtFireCount.getText().toString());
        actionInfo.setFireActionCount(TextUtils.isEmpty(mEtFireActionCount.getText().toString()) ? "0" : mEtFireActionCount.getText().toString());
        actionInfo.setFireCarCount(TextUtils.isEmpty(mEtFireCarCount.getText().toString()) ? "0" : mEtFireCarCount.getText().toString());
        actionInfo.setFirePeopleCount(TextUtils.isEmpty(mEtFirePeopleCount.getText().toString()) ? "0" : mEtFirePeopleCount.getText().toString());
        actionInfo.setFireDeadCount(TextUtils.isEmpty(mEtFireDeadCount.getText().toString()) ? "0" : mEtFireDeadCount.getText().toString());
        actionInfo.setFireSavedCount(TextUtils.isEmpty(mEtFireSavedCount.getText().toString()) ? "0" : mEtFireSavedCount.getText().toString());
        actionInfo.setFireInjuredCount(TextUtils.isEmpty(mEtFireInjuredCount.getText().toString()) ? "0" : mEtFireInjuredCount.getText().toString());
        actionInfo.setFireSurviveCount(TextUtils.isEmpty(mEtFireSurviveCount.getText().toString()) ? "0" : mEtFireSurviveCount.getText().toString());
        actionInfo.setFireEvacuateCount(TextUtils.isEmpty(mEtFireEvacuateCount.getText().toString()) ? "0" : mEtFireEvacuateCount.getText().toString());

        actionInfo.setRescueCount(TextUtils.isEmpty(mEtRescueCount.getText().toString()) ? "0" : mEtRescueCount.getText().toString());
        actionInfo.setRescueActionCount(TextUtils.isEmpty(mEtRescueActionCount.getText().toString()) ? "0" : mEtRescueActionCount.getText().toString());
        actionInfo.setRescueCarCount(TextUtils.isEmpty(mEtRescueCarCount.getText().toString()) ? "0" : mEtRescueCarCount.getText().toString());
        actionInfo.setRescuePeopleCount(TextUtils.isEmpty(mEtRescuePeopleCount.getText().toString()) ? "0" : mEtRescuePeopleCount.getText().toString());
        actionInfo.setRescueDeadCount(TextUtils.isEmpty(mEtRescueDeadCount.getText().toString()) ? "0" : mEtRescueDeadCount.getText().toString());
        actionInfo.setRescueSavedCount(TextUtils.isEmpty(mEtRescueSavedCount.getText().toString()) ? "0" : mEtRescueSavedCount.getText().toString());
        actionInfo.setRescueInjuredCount(TextUtils.isEmpty(mEtRescueInjuredCount.getText().toString()) ? "0" : mEtRescueInjuredCount.getText().toString());
        actionInfo.setRescueSurviveCount(TextUtils.isEmpty(mEtRescueSurviveCount.getText().toString()) ? "0" : mEtRescueSurviveCount.getText().toString());


    }

    @Override
    public void onSuccess(BaseBean data) {

    }

    @Override
    public void onSuccess(String message) {
        showShortToast("保存成功");
        finish();
    }

    @Override
    public void onFailure(String message) {
        showShortToast(message);
    }

    @Override
    public void showProgress() {
        startProgressDialog();
    }

    @Override
    public void hideProgress() {
        stopProgressDialog();
    }
}
